package com.xjx.controller;

import com.alibaba.fastjson.JSONObject;
import com.xjx.pojo.PageInfo;
import com.xjx.pojo.Provider;
import com.xjx.service.ProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/provider")
public class ProviderController {
    @Autowired
    private ProviderService providerService;
//
    @GetMapping("/findByPage")
    public ModelAndView findByPage(@RequestParam(defaultValue = "1")int currentPage,
                                   @RequestParam(defaultValue = "5")int size,
                                   @RequestParam(required = false) String proName,
                                   @RequestParam(required = false) String proCode,
                                   @RequestParam(required = false) String startDate,
                                   @RequestParam(required = false) String endDate){
        ModelAndView modelAndView = new ModelAndView();
        PageInfo<Provider> pageInfo = providerService.findByPage(currentPage,size,proName,proCode,startDate,endDate);
        modelAndView.setViewName("providerList");
        modelAndView.addObject("pageInfo",pageInfo);
        modelAndView.addObject("proName",proName);
        modelAndView.addObject("proCode",proCode);
        modelAndView.addObject("startDate",startDate);
        modelAndView.addObject("endDate",endDate);
        return modelAndView;
    }
    @ResponseBody
    @PostMapping("/save")
    public String save(String proCode,String proName,String  proContact,String proPhone,String proDesc){
        Provider provider = new Provider();
        provider.setProCode(proCode);
        provider.setProName(proName);
        provider.setProContact(proContact);
        provider.setProPhone(proPhone);
        provider.setProDesc(proDesc);
        JSONObject object = new JSONObject();
        if (providerService.save(provider)>0){
            object.put("code",200);
            object.put("message","success");
        }else {
            object.put("code",400);
            object.put("message","fail");
        }
        return object.toJSONString();
    }
    @GetMapping("/findById")
    public ModelAndView findById(Integer id){
        ModelAndView modelAndView = new ModelAndView();
        Provider provider = providerService.findById(id);
        modelAndView.addObject("provider",provider);
        modelAndView.setViewName("addProvider");
        return modelAndView;
    }
    @ResponseBody
    @PostMapping("/modify")
    public String modify(Integer id,String proName,String proCode,String proContact,String proPhone,String proDesc){
        Provider provider = new Provider();
        provider.setId(id);
        provider.setProCode(proCode);
        provider.setProName(proName);
        provider.setProContact(proContact);
        provider.setProPhone(proPhone);
        provider.setProDesc(proDesc);
        JSONObject object = new JSONObject();
        if (providerService.modify(provider)>0){
            object.put("code",200);
            object.put("message","success");
        }else {
            object.put("code",400);
            object.put("message","fail");
        }
        return object.toJSONString();
    }
    @ResponseBody
    @PostMapping("/delete")
    public String delete(Integer id){
        JSONObject object = new JSONObject();
        if(providerService.delete(id) > 0){
            object.put("code",200);
            object.put("message","success");
        }else {
            object.put("code",400);
            object.put("message","fail");
        }
        return object.toJSONString();
    }
}
